﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Ink;

namespace InkCanvasControl
{
    /// <summary>
    /// CInkCanvas2.xaml 的交互逻辑
    /// </summary>
    public partial class CInkCanvas2 : UserControl
    {
        Stroke str2;
        public CInkCanvas2()
        {
            InitializeComponent();
            Loaded += new RoutedEventHandler(CInkCanvas2_Loaded);
            Touch.FrameReported += new TouchFrameEventHandler(Touch_FrameReported);
        }

        void Touch_FrameReported(object sender, TouchFrameEventArgs e)
        {
            if (this.inkCanv != null && inkCanv.EditingMode == InkCanvasEditingMode.Ink)
            {
                foreach (TouchPoint _touchPoint in e.GetTouchPoints(this.inkCanv))
                {
                    if (_touchPoint.Action == TouchAction.Down)
                    {
                        _touchPoint.TouchDevice.Capture(this.inkCanv);
                    }

                    else if (_touchPoint.Action == TouchAction.Move && e.GetPrimaryTouchPoint(this.inkCanv) != null)
                    {
                        if (_touchPoint.TouchDevice.Id != e.GetPrimaryTouchPoint(this.inkCanv).TouchDevice.Id)
                        {
                            if (str2 != null && inkCanv.Strokes.Contains(str2))
                            {
                                str2.StylusPoints.Add(new StylusPoint(_touchPoint.Position.X, _touchPoint.Position.Y));
                            }
                            else
                            {
                                StylusPointCollection sc = new StylusPointCollection();
                                sc.Add(new StylusPoint(_touchPoint.Position.X, _touchPoint.Position.Y));
                                str2 = new Stroke(sc);
                                str2.DrawingAttributes = inkCanv.DefaultDrawingAttributes;
                                inkCanv.Strokes.Add(str2);
                            }
                        }
                    }

                    else if (_touchPoint.Action == TouchAction.Up)
                    {
                        if (_touchPoint.TouchDevice.Captured == this.inkCanv)
                        {
                            str2 = null;
                            this.inkCanv.ReleaseTouchCapture(_touchPoint.TouchDevice);
                        }
                    }
                }
            }
        }

        void CInkCanvas2_Loaded(object sender, RoutedEventArgs e)
        {
            SetInk(1, Colors.Black);
            CurrentColor.Background = new SolidColorBrush() { Color = Colors.Black };
            SetBasicColors();
            SelectBrushThickness.ToolTip = "当前笔刷粗细：1px";
        }

        #region 私有变量
        private SolidColorBrush _mainBackground = new SolidColorBrush();
        string[] str = new string[] { "FF", "CC", "99", "66", "33", "00" };
        #endregion

        private void RadioButton_Click(object sender, RoutedEventArgs e)
        {
            RadioButton rb = sender as RadioButton;
            SetInk(rb.FontSize, inkCanv.DefaultDrawingAttributes.Color);
            SelectBrushThickness.P_Popup.IsOpen = false;
        }

        private void SetInk(double strokeThickness,Color c)
        {
            DrawingAttributes inkDA = new DrawingAttributes();
            inkDA.Width = inkDA.Height = strokeThickness;
            inkDA.Color = c;
            inkDA.IsHighlighter = inkCanv.DefaultDrawingAttributes.IsHighlighter;
            inkCanv.DefaultDrawingAttributes = inkDA;
            CurrentColor.Background = new SolidColorBrush() { Color = c };
            SelectBrushThickness.ToolTip = "当前笔刷粗细："+strokeThickness+"px";
        }

        #region 填充选色板及相关处理事件
        void SetBasicColors()
        {
            #region 填充右侧基本颜色面板
            for (int i = 0; i < 12; i++)
            {
                for (int j = 0; j < 18; j++)
                {
                    if (i < 6)
                    {
                        if (j < 6)
                        {
                            addChildren(this.right_root, "CC", str[i], str[j]);
                        }
                        if (j >= 6 && j < 12)
                        {
                            addChildren(this.right_root, "66", str[i], str[11 - j]);
                        }
                        if (j >= 12 && j < 18)
                        {
                            addChildren(this.right_root, "00", str[i], str[j - 12]);
                        }
                    }
                    else
                    {
                        if (j < 6)
                        {
                            addChildren(this.right_root, "FF", str[11 - i], str[j]);
                        }
                        if (j >= 6 && j < 12)
                        {
                            addChildren(this.right_root, "99", str[11 - i], str[11 - j]);
                        }
                        if (j >= 12 && j < 18)
                        {
                            addChildren(this.right_root, "33", str[11 - i], str[j - 12]);
                        }
                    }
                }
            }
            #endregion

            #region 填充左侧基本颜色面板
            for (int i = 0; i < 3; i++)
            {
                for (int j = 0; j < 12; j++)
                {
                    if (i == 0 || i == 2)
                    {
                        addChildren(this.left_root, "00", "00", "00");
                    }
                    else
                    {
                        if (j < 6)
                        {
                            addChildren(this.left_root, str[5 - j], str[5 - j], str[5 - j]);
                        }
                        else
                        {
                            switch (j)
                            {
                                case 6: { addChildren(this.left_root, "FF", "00", "00"); break; }
                                case 7: { addChildren(this.left_root, "00", "FF", "00"); break; }
                                case 8: { addChildren(this.left_root, "00", "00", "FF"); break; }
                                case 9: { addChildren(this.left_root, "FF", "FF", "00"); break; }
                                case 10: { addChildren(this.left_root, "00", "FF", "FF"); break; }
                                case 11: { addChildren(this.left_root, "FF", "00", "FF"); break; }
                            }
                        }
                    }
                }
            }
            #endregion
        }
        void addChildren(WrapPanel wp, string colorR, string colorG, string colorB)
        {
            Border bd = new Border();
            bd.Background = new SolidColorBrush((Color)ColorConverter.ConvertFromString("#" + colorR + colorG + colorB));
            bd.BorderThickness = new Thickness(1, 1, 0, 0);
            bd.BorderBrush = Brushes.Black;
            bd.ToolTip = "#" + colorR + colorG + colorB;
            bd.SnapsToDevicePixels = true;
            bd.MouseEnter += new MouseEventHandler(bd_MouseEnter);
            bd.MouseLeave += new MouseEventHandler(bd_MouseLeave);
            bd.MouseDown += new MouseButtonEventHandler(bd_MouseDown);
            wp.Children.Add(bd);
        }

        void bd_MouseDown(object sender, MouseButtonEventArgs e)
        {
            Color selectColor = (Color)ColorConverter.ConvertFromString("#" + (sender as Border).Background.ToString().Substring(3, 6));
            SetInk(inkCanv.DefaultDrawingAttributes.Width, selectColor);
            SelectBrush.P_Popup.IsOpen = false;
        }

        void bd_MouseLeave(object sender, MouseEventArgs e)
        {
            (sender as Border).Child = null;
        }

        void bd_MouseEnter(object sender, MouseEventArgs e)
        {
            Border bd2 = new Border();
            bd2.Background = Brushes.Transparent;
            bd2.BorderThickness = new Thickness(1);
            if ((sender as Border).Background.ToString() == Brushes.Black.ToString())
            {
                bd2.BorderBrush = Brushes.White;
            }
            else if ((sender as Border).Background.ToString() == "#FF333366" ||
                (sender as Border).Background.ToString() == "#FF333333")
            {
                bd2.BorderBrush = Brushes.White;
            }
            else if ((sender as Border).Background.ToString() == "#FF666666"
                || (sender as Border).Background.ToString() == "#FF999999"
                || (sender as Border).Background.ToString() == "#FFCCCCCC"
                || (sender as Border).Background.ToString() == "#FFFFFFFF")
            {
                bd2.BorderBrush = Brushes.OrangeRed;
            }
            else
            {
                byte _r = ((SolidColorBrush)((sender as Border).Background)).Color.R;
                byte _g = ((SolidColorBrush)((sender as Border).Background)).Color.G;
                byte _b = ((SolidColorBrush)((sender as Border).Background)).Color.B;
                byte r = 30;
                byte g = 30;
                byte b = 30;
                if (_r == 0)
                {
                    r = 255;
                }
                if (_g == 0)
                {
                    g = 255;
                }
                if (_b == 0)
                {
                    b = 255;
                }
                bd2.BorderBrush = new SolidColorBrush((Color)Color.FromRgb(r, g, b));
            }


            (sender as Border).Child = bd2;
        }
        #endregion
    }
}
